package cn.itcast.wushangxiantu.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.wushangxiantu.BaseAdapter.FuBenViewPagerAdapter;
import cn.itcast.wushangxiantu.Fragment.FuBenChuangjian;
import cn.itcast.wushangxiantu.Fragment.FuBenDaTin;
import cn.itcast.wushangxiantu.UserData.FuBenNum;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DuoRenFuBen extends AppCompatActivity implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private List<Fragment> viewList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();
    private FuBenViewPagerAdapter adapter;
    private TextView tvCJNum;
    private TextView tvJRnum;
    private TextView tvJLnum;
    private TextView tvGMnum;
    private Button btnGM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duo_ren_fu_ben);
        initView();
        //注册
        EventBus.getDefault().register(this);
    }

    private void initView() {
        titleList.add("秘境大厅");
        titleList.add("创建房间");
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        addView();

        tvCJNum = (TextView) findViewById(R.id.tvCJNum);
        tvJRnum = (TextView) findViewById(R.id.tvJRnum);
        tvJLnum = (TextView) findViewById(R.id.tvJLnum);
        tvGMnum = (TextView) findViewById(R.id.tvGMnum);

        getNum(Login.getUser().getUsernumber());
        btnGM = (Button) findViewById(R.id.btnGM);
        btnGM.setOnClickListener(this);
    }

    private void getNum(String usernumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumber + "&need=DuoRenFuBenNum", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("fubennum1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("fubennum2", src);
                FuBenNum fuBenNum = null;
                try {
                    fuBenNum = new Gson().fromJson(src, FuBenNum.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = fuBenNum;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    private void gmJL(String usernumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumber + "&need=DuoRenFuBenGouMai", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("fubennum1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("fubennum2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    FuBenNum fuBenNum = (FuBenNum) message.obj;
                    if (null != fuBenNum) {
                        tvCJNum.setText("今日剩余建房次数" + fuBenNum.getCjnum());
                        tvGMnum.setText("今日剩余购买次数" + fuBenNum.getGmnum());
                        tvJLnum.setText("今日剩余奖励次数" + fuBenNum.getJlnum());
                        tvJRnum.setText("今日剩余加入次数" + fuBenNum.getJrnum());
                    } else {
                        ToastUtil.toast(DuoRenFuBen.this, "服务器炸了");
                    }
                    break;
                case 2:
                    User user=(User)message.obj;
                    if (null==user){
                        ToastUtil.toast(DuoRenFuBen.this, "服务器炸了");
                    }else {
                        GeneralDialog("购买详情",user.getStw(),0);
                        getNum(Login.getUser().getUsernumber());
                    }
                    break;
            }
            return false;
        }
    });

    public void GeneralDialog(String head, String content, final int Num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(head)

                .setMessage(content)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        if (Num == 1) {
                            gmJL(Login.getUser().getUsernumber());
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void addView() {
        viewList.add(new FuBenDaTin(DuoRenFuBen.this));
        viewList.add(new FuBenChuangjian(DuoRenFuBen.this));
        adapter = new FuBenViewPagerAdapter(getSupportFragmentManager(), viewList, titleList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        EventBus.getDefault().unregister(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnGM:
                GeneralDialog("购买奖励次数","确定花费10石魂购买一次奖励次数",1);
                break;
        }
    }
    /**
     * 接收后处理
     *
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shuaxing(MessageEvent messageEvent) {
        if (messageEvent.getMessage().equals("getFuBenNum") ) {
           getNum(Login.getUser().getUsernumber());
        }
    }

}